package com.zy.nft_system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zy.nft_system.domain.*;
import com.zy.nft_system.domain.dto.AccountDto;
import com.zy.nft_system.domain.dto.QntiqueauthDto;
import com.zy.nft_system.domain.dto.ZengAntique;
import com.zy.nft_system.exception.MyException;
import com.zy.nft_system.mapper.UserantiqueMapper;
import com.zy.nft_system.service.QntiqueauthService;
import com.zy.nft_system.mapper.QntiqueauthMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
* @author 这是那头猪？
* @description 针对表【t_qntiqueauth(藏品权限表)】的数据库操作Service实现
* @createDate 2022-12-07 15:43:02
*/
@Slf4j
@Service
public class QntiqueauthServiceImpl extends ServiceImpl<QntiqueauthMapper, Qntiqueauth>
    implements QntiqueauthService{

    @Resource
    QntiqueauthMapper qntiqueauthMapper;
    @Resource
    UserantiqueMapper userantiqueMapper;
    @Override
    public Result listCollection(QntiqueauthDto qntiqueauthDto) {
      List<QntiqueauthDto> qntiqueauthDtos= qntiqueauthMapper.listCollection(qntiqueauthDto);
        if (qntiqueauthDtos == null) {
            log.info("未查找到藏品权限");
            return Result.error("未查找到藏品权限");
        }
        log.info("藏品权限查找成功");
        return Result.success("藏品权限查找成功",qntiqueauthDtos);
    }

    @Override
    public Result listCollectionStatus1() {
        List<Antique> antiques= qntiqueauthMapper.listCollectionStatus1();
        if (antiques == null) {
            log.info("未找到在售藏品，请换个事件段再来");
            return Result.error("未找到在售藏品，请换个事件段再来");
        }
        log.info("查找在售藏品成功");
        return Result.success("查找在售藏品成功",antiques);
    }

    @Override
    public Result queryCoolectionById(int id) {
        Antique antique=qntiqueauthMapper.queryCoolectionById(id);
        if (antique == null) {
            log.info("查询失败，非法请求");
            return Result.error("非法请求");
        }
        log.info("查询成功");
        return Result.success("查询成功", antique);
    }

    @Override
    @Transactional(rollbackFor = MyException.class)
    public Result zhuangzeng(ZengAntique zengAntique) {
        log.info("正在查询用户要装增藏品的信息");
        QueryWrapper<Userantique> eq = new QueryWrapper<Userantique>().eq("uid", zengAntique.getUid()).eq("aid", zengAntique.getAid()).eq("id", zengAntique.getId());
        Userantique userantique = userantiqueMapper.selectOne(eq);
        if (userantique == null) {
            log.info("未找到要转增的藏品");
            throw new MyException("网络原因转增失败");
        }
        log.info("正在进行转赠");
        int delete = userantiqueMapper.delete(eq);
        if (delete != 1) {
            log.info("移除转增人藏品失败");
            throw new MyException("网络原因转增失败");
        }
        userantique.setUid(zengAntique.getZid());
        userantique.setId(null);
        userantique.setCreatetime(null);
        int insert = userantiqueMapper.insert(userantique);
        if (insert != 1) {
            log.info("将藏品添加给受赠人失败");
            throw new MyException("网络原因转增失败");
        }
        log.info("转赠成功");
        return Result.success("转赠成功",null);
    }
}




